﻿
namespace SuperMarieApi.App.Controllers.Tool.Extensions
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Net;
    using System.Security.Cryptography;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;

    using SuperMarieApi.App.Controllers.Framework.XmlConfig.Model;
    using SuperMarieApi.App.Controllers.Framework.XmlConfig.Service;

    public static class StringExte
    {
        static RuntimeProperties runtimeProperties = new RuntimePropertiesHelper().loadConfig();
        public static string MobileNumber = @"^1(3|4|5|7|8)\d{9}$";

        /// <summary>
        /// 字符串如果操过指定长度则将超出的部分用指定字符串代替
        /// </summary>
        /// <param name="p_SrcString">要检查的字符串</param>
        /// <param name="p_Length">指定长度</param>
        /// <param name="p_TailString">用于替换的字符串</param>
        /// <returns>截取后的字符串</returns>
        public static string GetSubString(this string p_SrcString, int p_Length, string p_TailString)
        {
            return GetSubString(p_SrcString, 0, p_Length, p_TailString);
        }

        /// <summary>
        /// 取指定长度的字符串
        /// </summary>
        /// <param name="p_SrcString">要检查的字符串</param>
        /// <param name="p_StartIndex">起始位置</param>
        /// <param name="p_Length">指定长度</param>
        /// <param name="p_TailString">用于替换的字符串</param>
        /// <returns>截取后的字符串</returns>
        public static string GetSubString(this string p_SrcString, int p_StartIndex, int p_Length, string p_TailString)
        {


            string myResult = p_SrcString;

            //当是日文或韩文时(注:中文的范围:\u4e00 - \u9fa5, 日文在\u0800 - \u4e00, 韩文为\xAC00-\xD7A3)
            if (System.Text.RegularExpressions.Regex.IsMatch(p_SrcString, "[\u0800-\u4e00]+") ||
                System.Text.RegularExpressions.Regex.IsMatch(p_SrcString, "[\xAC00-\xD7A3]+"))
            {
                //当截取的起始位置超出字段串长度时
                if (p_StartIndex >= p_SrcString.Length)
                {
                    return "";
                }
                else
                {
                    return p_SrcString.Substring(p_StartIndex,
                                                   ((p_Length + p_StartIndex) > p_SrcString.Length) ? (p_SrcString.Length - p_StartIndex) : p_Length);
                }
            }


            if (p_Length >= 0)
            {
                byte[] bsSrcString = Encoding.Default.GetBytes(p_SrcString);

                //当字符串长度大于起始位置
                if (bsSrcString.Length > p_StartIndex)
                {
                    int p_EndIndex = bsSrcString.Length;

                    //当要截取的长度在字符串的有效长度范围内
                    if (bsSrcString.Length > (p_StartIndex + p_Length))
                    {
                        p_EndIndex = p_Length + p_StartIndex;
                    }
                    else
                    {   //当不在有效范围内时,只取到字符串的结尾

                        p_Length = bsSrcString.Length - p_StartIndex;
                        p_TailString = "";
                    }



                    int nRealLength = p_Length;
                    int[] anResultFlag = new int[p_Length];
                    byte[] bsResult = null;

                    int nFlag = 0;
                    for (int i = p_StartIndex; i < p_EndIndex; i++)
                    {

                        if (bsSrcString[i] > 127)
                        {
                            nFlag++;
                            if (nFlag == 3)
                            {
                                nFlag = 1;
                            }
                        }
                        else
                        {
                            nFlag = 0;
                        }

                        anResultFlag[i] = nFlag;
                    }

                    if ((bsSrcString[p_EndIndex - 1] > 127) && (anResultFlag[p_Length - 1] == 1))
                    {
                        nRealLength = p_Length + 1;
                    }

                    bsResult = new byte[nRealLength];

                    Array.Copy(bsSrcString, p_StartIndex, bsResult, 0, nRealLength);

                    myResult = Encoding.Default.GetString(bsResult);

                    myResult = myResult + p_TailString;
                }
            }

            return myResult;
        }

        /// <summary>
        /// 过滤HTML中的不安全标签
        /// </summary>
        /// <param name="content">传入需要过滤的字符串</param>
        /// <returns>返回过滤后的html</returns>
        public static string RemoveUnsafeHtml(this string content)
        {
            content = Regex.Replace(content, @"(\<|\s+)o([a-z]+\s?=)", "$1$2", RegexOptions.IgnoreCase);
            content = Regex.Replace(content, @"(script|frame|form|meta|behavior|style)([\s|:|>])+", "$1.$2", RegexOptions.IgnoreCase);
            return content;
        }

        /// <summary>
        /// 过滤不安全sql字符串
        /// </summary>
        /// <param name="s">传入的字符串</param>
        /// <returns>返回经过过滤处理后的字符串</returns>
        public static string FilteUnSafeSqlStr(this string str)
        {
            str = Regex.Replace(str, @"delete ", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"update ", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"insert ", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"select ", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"drop ", "", RegexOptions.IgnoreCase);
            //应用站点的生成字符过滤调整
            //str = Regex.Replace(str, @"count", "", RegexOptions.IgnoreCase);
            //str = Regex.Replace(str, @"or ", "", RegexOptions.IgnoreCase);
            //str = Regex.Replace(str, @"and ", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"truncate ", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"asc", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"desc", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"xp_cmdshell ", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"exec master ", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"net localgroup administrators ", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"net user ", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"Exec ", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"Execute ", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"xp_", "x p_", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"sp_", "s p_", RegexOptions.IgnoreCase);
            return str;
        }

        #region unicode操作
        /// <summary>
        /// 转化成unicode编码输出
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToUnicode(this string s)
        {
            char[] charbuffers = s.ToCharArray();
            byte[] buffer;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < charbuffers.Length; i++)
            {
                buffer = System.Text.Encoding.Unicode.GetBytes(charbuffers[i].ToString());
                sb.Append(String.Format("\\u{0:X2}{1:X2}", buffer[1], buffer[0]));
            }
            return sb.ToString();
        }

        /// <summary>
        /// 解码 Unicode
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string UnUnicode(this string s)
        {
            Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
            return reg.Replace(s, delegate(Match m)
            {
                return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString();
            });
        }
        #endregion

        #region 加解密
        /// <summary>
        /// MD5函数
        /// </summary>
        /// <param name="str">原始字符串</param>
        /// <returns>MD5结果</returns>
        public static string ToMD5(this string str)
        {
            string newString = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
            return newString;
        }

        //默认密钥向量
        private static byte[] Keys1 = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

        /// <summary>
        /// DES加密字符串
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="encryptKey">加密密钥,要求为8位</param>
        /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
        public static string DESEncode(this string encryptString, string encryptKey)
        {
            encryptKey = encryptKey.GetSubString(8, "");
            encryptKey = encryptKey.PadRight(8, ' ');
            byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
            byte[] rgbIV = Keys1;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray());

        }

        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
        /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
        public static string DESDecode(this string decryptString, string decryptKey)
        {
            try
            {
                decryptKey = decryptKey.GetSubString(8, "");
                decryptKey = decryptKey.PadRight(8, ' ');
                byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
                byte[] rgbIV = Keys1;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();

                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return "";
            }

        }
        #endregion


        /// <summary>
        /// 检测是否符合IsMobile格式
        /// </summary>
        /// <param name="strEmail">要判断的mobile字符串</param>
        /// <returns>true 表示是,false表示不是</returns>
        public static bool IsMobile(this string mobile)
        {
            return Regex.IsMatch(mobile, MobileNumber);
        }


        /// <summary>
        /// 检测是否符合email格式
        /// </summary>
        /// <param name="strEmail">要判断的email字符串</param>
        /// <returns>true 表示是,false表示不是</returns>
        public static bool IsEmail(this string strEmail)
        {
            return Regex.IsMatch(strEmail, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        }

        /// <summary>
        /// string型转换为bool型
        /// </summary>
        ///<param name="s">传入的字符串</param>
        /// <param name="defValue">缺省值，如果需要转换对象为 null 类型或是不能转换的则返回缺省值</param>
        /// <returns>转换后的bool类型结果</returns>
        public static bool ToBoolean(this string s, bool defValue)
        {
            if (s != null)
            {
                if (string.Compare(s.ToUpper(), "TRUE", true) == 0)
                {
                    return true;
                }
                else if (string.Compare(s.ToUpper(), "FALSE", true) == 0)
                {
                    return false;
                }
            }
            return defValue;
        }

        /// <summary>
        /// 将对象转换为Int64类型
        /// </summary>
        /// <param name="defValue">缺省值，如果需要转换对象为 null 类型或是不能转换的则返回缺省值</param>
        /// <param name="s">传入的字符串</param>
        /// <returns>转换后的Int64类型结果</returns>
        public static Int64 ToInt64(this object s, long defValue)
        {

            if (s != DBNull.Value && s != null)
            {
                // if (s.ToString().Length > 0 && s.ToString().Length <= 11 && Regex.IsMatch(s.ToString(), @"^((-[1-9])|([1-9]))\d*$"))//
                if (s.ToString().Length > 0 && s.ToString().Length <= 11 && Regex.IsMatch(s.ToString(), @"^[-]?[0-9]*$"))
                {
                    if ((s.ToString().Length < 19) || (s.ToString().Length == 19 && s.ToString()[0] == '1') || (s.ToString().Length == 20 && s.ToString()[0] == '-' && s.ToString()[1] == '1'))
                    {
                        //todo:npoi单元格报错 可以加类型判断 能Convert的才Convert.ToInt32 否则先tostring
                        return Convert.ToInt64(s);
                    }
                }
            }
            return defValue;
        }

        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="defValue">缺省值，如果需要转换对象为 null 类型或是不能转换的则返回缺省值</param>
        /// <param name="s">传入的字符串</param>
        /// <returns>转换后的Int32类型结果</returns>
        public static Int32 ToInt32(this string s, int defValue)
        {

            if (s != null)
            {
                //if (s.Length > 0 && s.Length <= 11 && Regex.IsMatch(s, @"^((-[1-9])|([1-9]))\d*$"))//
                if (s.Length > 0 && s.Length <= 11 && Regex.IsMatch(s, @"^[-]?[0-9]*$"))
                {
                    if ((s.Length < 10) || (s.Length == 10 && s[0] == '1') || (s.Length == 11 && s[0] == '-' && s[1] == '1'))
                    {
                        return Convert.ToInt32(s);
                    }
                }
            }
            return defValue;
        }

        /// <summary>
        /// 判断是否为ip
        /// </summary>
        /// <param name="ip">需要判断的字符串</param>
        /// <returns>返回true表示是IP</returns>
        public static bool IsIP(this string ip)
        {
            return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");

        }

        /// <summary>
        /// 检查处理当前字符串中有害的SQL脚本，防止SQL注入
        /// </summary>
        /// <param name="s">传入的字符串</param>
        /// <returns>返回经过过滤处理后的字符串</returns>
        public static string SqlCheck(this string s)
        {

            s = s.Replace("delete ", "");
            s = s.Replace("update ", "");
            s = s.Replace("insert ", "");
            s = s.Replace("drop ", "");

            //去除执行存储过程的命令关键字
            s = s.Replace("Exec ", "");
            s = s.Replace("Execute ", "");

            //去除系统存储过程或扩展存储过程关键字
            s = s.Replace("xp_", "x p_");
            s = s.Replace("sp_", "s p_");

            ////防止16进制注入
            //s = s.Replace("0x", "0 x");
            return s;
        }

        /// <summary>
        /// IP地址转成long
        ///  IP通过补位方式转成long值
        /// </summary>
        /// <param name="bt"></param>
        /// <returns></returns>
        public static long IP2Long(string sips)
        {
            int x = 3;
            long o = 0;
            string[] _sip = sips.Split('.');
            if (_sip.Length != 4)
            {
                return o;
            }

            byte[] bt = new byte[4] { Convert.ToByte(_sip[0]), Convert.ToByte(_sip[1]), Convert.ToByte(_sip[2]), Convert.ToByte(_sip[3]) };

            foreach (byte f in bt)
            {
                o += (long)f << 8 * x--;
            }
            return o;

        }

        /// <summary>
        /// string型转换为decimal型
        /// </summary>
        /// <param name="s">传入的字符串</param>
        /// <param name="defValue">缺省值，如果需要转换对象为 null 类型或是不能转换的则返回缺省值</param>
        /// <returns>转换后的decimal类型结果</returns>
        public static decimal ToDecimal(this string s, decimal defValue)
        {
            if ((s == null))
            {
                return defValue;
            }

            decimal value = defValue;

            if (s != null)
            {
                //bool isDecimal = Regex.IsMatch(s, @"^(([1-9]\d*\.\d*|0\.\d*[1-9])|-([1-9]\d*\.\d*|0\.\d*[1-9]))\d*$");
                bool isDecimal = Regex.IsMatch(s, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                if (isDecimal)
                {
                    value = Convert.ToDecimal(s);
                }
            }
            return value;
        }

        /// <summary>
        /// 转换为string类型
        /// </summary>
        /// <param name="s">要转换的数据</param>
        /// <param name="defValue">默认值</param>
        /// <returns>s不为NULL或DBNULL时 返回s.ToString(),否则 返回传入defValue值</returns>
        public static string ToString(this object s, string defValue)
        {
            if (s != null && s != DBNull.Value)
            {


                return s.ToString();

            }
            else
            {
                return defValue;
            }
        }


        /// <summary>
        /// 生成验证字符串识别码（用于身份验证）
        /// </summary>
        /// <param name="str">要判断字符串</param>
        /// <param name="variable">可变控制</param>
        /// <returns>true通过验证 false没通过验证</returns>
        public static string VCodeCreate(this string str, int expires)
        {
            //return ((str + DateTime.Now.AddMinutes(expires).ToString("yyyyMMddhhmm")).ToSHA256().ToMD5() + DateTime.Now.AddMinutes(expires).ToString("yyyyMMddhhmm") + "S" + expires).DESEncode(runtimeProperties.Encryptkey);

            return (expires + str.ToMD5()).ToMD5() + expires;
        }

        /// <summary>
        /// SHA256函数
        /// </summary>
        /// /// <param name="str">原始字符串</param>
        /// <returns>SHA256结果</returns>
        public static string ToSHA256(this string str)
        {
            byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
            SHA256Managed Sha256 = new SHA256Managed();
            byte[] Result = Sha256.ComputeHash(SHA256Data);
            return Convert.ToBase64String(Result);  //返回长度为44字节的字符串
        }



        /// <summary>
        /// 判断对象是否为Int32类型的数字的字符器（包括正整数，负整数，正浮点型，负浮点型）可以转换和计算的
        /// </summary>
        /// <param name="s">传入的字符串</param>
        /// <returns>返回 true 表示为Int32类型的字符器，false 表示不是</returns>
        public static bool IsNumeric(this string s)
        {
            if (s != null)
            {
                bool bl1 = Regex.IsMatch(s, @"^(([1-9]\d*\.\d*|0\.\d*[1-9])|(-[1-9])|([1-9])|-([1-9]\d*\.\d*|0\.\d*[1-9]))\d*$");

                if (s.Length > 0 && s.Length <= 11 && bl1)
                {
                    if ((s.Length < 10) || (s.Length == 10 && s[0] == '1') || (s.Length == 11 && s[0] == '-' && s[1] == '1'))
                    {
                        return true;
                    }
                }
            }
            return false;

        }



        /// <summary>
        /// 判断当前字符串是否为Double类型的字符器
        /// </summary>
        /// <param name="s">传入的字符串</param>
        /// <returns>返回true表示是Double类型的字符器</returns>
        public static bool IsDouble(this string s)
        {
            if (s != null)
            {
                return Regex.IsMatch(s, @"^(([1-9]\d*\.\d*|0\.\d*[1-9])|(-[1-9])|([1-9])|-([1-9]\d*\.\d*|0\.\d*[1-9]))\d*$");//return Regex.IsMatch(s, @"^([0-9])[0-9]*(\.\w*)?$");
            }
            return false;
        }


        /// <summary>
        /// 将对象转换为Int16类型
        /// </summary>
        /// <param name="defValue">缺省值，如果需要转换对象为 null 类型或是不能转换的则返回缺省值</param>
        /// <param name="s">传入的字符串</param>
        /// <returns>转换后的Int16类型结果</returns>
        public static Int16 ToInt16(this string s, Int16 defValue)
        {

            if (s != null)
            {
                //if (s.Length > 0 && s.Length <= 11 && Regex.IsMatch(s, @"^((-[1-9])|([1-9]))\d*$"))//  
                if (s.Length > 0 && s.Length <= 11 && Regex.IsMatch(s, @"^[-]?[0-9]*$"))
                {
                    if ((s.Length < 10) || (s.Length == 10 && s[0] == '1') || (s.Length == 11 && s[0] == '-' && s[1] == '1'))
                    {
                        return Convert.ToInt16(s);
                    }
                }
            }
            return defValue;
        }

        /// <summary>
        /// string型转换为float型(长度小于10,否则返回默认值)
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值，如果需要转换对象为 null 类型或是不能转换的则返回缺省值</param>
        /// <returns>转换后的float类型结果</returns>
        public static float ToFloat(this string strValue, float defValue)
        {
            if ((strValue == null) || (strValue.Length > 10))
                return defValue;

            float floValue = defValue;
            if (strValue != null)
            {
                //bool IsFloat = Regex.IsMatch(strValue, @"^(([1-9]\d*\.\d*|0\.\d*[1-9])|(-[1-9])|([1-9])|-([1-9]\d*\.\d*|0\.\d*[1-9]))\d*$");
                bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                if (IsFloat)
                    float.TryParse(strValue, out floValue);
            }
            return floValue;
        }

        /// <summary>
        /// string型转换为float型(忽略字符串长度,转换为等效单精度浮点数字,否则返回默认值)
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值，如果需要转换对象为 null 类型或是不能转换的则返回缺省值</param>
        /// <returns>转换后的float类型结果</returns>
        public static float ToFloatIgnoreLength(this string strValue, float defValue)
        {
            float floValue = defValue;
            if (strValue != null)
            {
                bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                if (IsFloat)
                    float.TryParse(strValue, out floValue);
            }
            return floValue;
        }

        /// <summary>
        /// string型转换为Single型
        /// </summary>
        /// <param name="s">传入的字符串</param>
        /// <param name="defValue">缺省值，如果需要转换对象为 null 类型或是不能转换的则返回缺省值</param>
        /// <returns>转换后的Single类型结果</returns>
        public static Single ToSingle(this string s, Single defValue)
        {
            if ((s == null) || (s.Length > 10))
            {
                return defValue;
            }

            Single value = defValue;
            {
                //bool isFloat = Regex.IsMatch(s, @"^(([1-9]\d*\.\d*|0\.\d*[1-9])|-([1-9]\d*\.\d*|0\.\d*[1-9]))\d*$");
                bool isFloat = Regex.IsMatch(s, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                if (isFloat)
                {
                    value = Convert.ToSingle(s);
                }
            }
            return value;
        }

        /// <summary>
        /// string型转换为DateTime类型
        /// </summary>
        /// <param name="s">传入的字符串</param>
        /// <param name="defValue">缺省值，如果需要转换对象为 null 类型或是不能转换的则返回缺省值</param>
        /// <returns>转换后的DateTime类型结果</returns>
        public static DateTime ToDateTime(this string s, DateTime defValue)
        {
            if (s == null)
            {
                return defValue;
            }
            else
            {
                try
                {
                    return Convert.ToDateTime(s);
                }
                catch
                {
                    return defValue;
                }
            }
        }

        /// <summary>
        /// string型转换为DateTime类型，如果不能转换则返回 null 
        /// </summary>
        /// <param name="s">传入的字符串</param>
        /// <returns>转换后的DateTime类型结果</returns>
        public static DateTime? ToDateTime(this string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return null;
            }
            else
            {
                try
                {
                    return Convert.ToDateTime(s);
                }
                catch
                {
                    return null;
                }
            }
        }

        /// <summary>       
        /// 将字符串内容转化为16进制数据编码，其逆过程是Decode 
        /// </summary>         
        /// <param name="strEncode">需要转化的原始字符串        
        /// 转换的过程是直接把字符转换成Unicode字符,比如数字"3"-->0033,汉字"我"-->U+6211         
        /// 函数decode的过程是encode的逆过程.</param>
        /// <returns>返回转化后的结果</returns>         
        public static string EncodeToHex16(this string strEncode)
        {
            string strReturn = "";//  存储转换后的编码             
            foreach (short shortx in strEncode.ToCharArray())
            {
                strReturn += shortx.ToString("X4");
            }
            return strReturn;
        }

        /// <summary>             
        /// 将16进制数据编码转化为字符串，是EncodeToHex16的逆过程         
        /// </summary>         
        /// <param name="strDecode">需要转化的原始字符串</param>         
        /// <returns>返回转化后的结果</returns>         
        public static string Hex16Decode(this string strDecode)
        {
            string sResult = "";
            for (int i = 0; i < strDecode.Length / 4; i++)
            {
                sResult += (char)short.Parse(strDecode.Substring(i * 4, 4), global::System.Globalization.NumberStyles.HexNumber);
            }
            return sResult;
        }


        /// <summary>
        /// 返回字符串字符长度, 1个汉字长度为2个长度，英文为1个长度
        /// </summary>
        /// <returns>字符长度</returns>
        public static int GetStringLength(this string str)
        {
            return Encoding.Default.GetBytes(str).Length;
        }





        /// <summary>
        /// 检测是否是正确的Url
        /// </summary>
        /// <param name="strUrl">要验证的Url</param>
        /// <returns>true 表示是,false表示不是</returns>
        public static bool IsURL(this string strUrl)
        {
            return Regex.IsMatch(strUrl, @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
        }

        /// <summary>
        /// 判断是否为base64字符串
        /// </summary>
        /// <param name="str">需要判断的字符串</param>
        /// <returns>true 表示是,false表示不是</returns>
        public static bool IsBase64String(this string str)
        {
            //A-Z, a-z, 0-9, +, /, =
            return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]");
        }

        /// <summary>
        /// 移除掉字符串第一个指定类型字符，（比如，）如果第一个字符与指定字符不同则返回原字符串，如果为null则返回""字符串
        /// </summary>
        /// <param name="strInput">输入字符null时返回""</param>
        /// <param name="str">要截取的指定字符串</param>
        /// <returns>返回截取后的字符串</returns>
        public static string CutFirstChar(this string strInput, string str)
        {
            if (!string.IsNullOrEmpty(strInput))
            {
                while (strInput.StartsWith(str))
                {
                    strInput = strInput.Remove(0, 1);
                }
                return strInput;
            }
            return "";
        }

        /// <summary>
        /// 移除掉字符串最后一个指定类型字符，（比如，）如果最后一个字符与指定字符不同则返回原字符串，如果为null则返回""字符串
        /// </summary>
        /// <param name="strInput">输入字符，null时返回""</param>
        /// <param name="str">要截取的字符串</param>
        /// <returns>返回截取后的字符串</returns>
        public static string CutLastChar(this string strInput, string str)
        {
            if (!string.IsNullOrEmpty(strInput))
            {
                while (strInput.EndsWith(str))
                {
                    strInput = strInput.Remove(strInput.Length - 1, 1);
                }
                return strInput;
            }
            return "";
        }

        /// <summary>
        /// 根据指定的长度截取字符串，大于指定长度的部分用...替换
        /// </summary>
        /// <param name="strInput">字符串</param>
        /// <param name="len">长度</param>
        /// <returns>返回截取后的字符串</returns>
        public static string ToCut(this string strInput, int len)
        {
            if (!string.IsNullOrEmpty(strInput))
            {
                strInput = strInput.Trim();
                int byteLen = Encoding.GetEncoding("gb2312").GetByteCount(strInput);
                if (byteLen > len)
                {
                    string resultStr = "";
                    for (int i = 0; i < strInput.Length; i++)
                    {
                        if (Encoding.GetEncoding("gb2312").GetByteCount(resultStr) < len - 2)
                        {
                            resultStr += strInput.Substring(i, 1);
                        }
                        else
                        {
                            break;
                        }
                    }
                    return resultStr + "...";
                }
                else
                {
                    return strInput;
                }
            }
            return "";
        }

        /// <summary>
        /// 截取字符串从指定位置开始，截取后的字符串超过指定长度的部分用，指定参数替换
        /// </summary>
        /// <param name="p_SrcString">要截取的原始字符串</param>
        /// <param name="p_StartIndex">要截取的起始位置</param>
        /// <param name="p_Length">指定长度，超过后的部分用 p_TailString替换</param>
        /// <param name="p_TailString">用于替换的字符串</param>
        /// <returns>返回截取拼接后的字符串</returns>
        public static string ToCut(this string p_SrcString, int p_StartIndex, int p_Length, string p_TailString)
        {
            string myResult = p_SrcString;

            if (p_Length >= 0)
            {
                byte[] bsSrcString = Encoding.Default.GetBytes(p_SrcString);

                //当字符串长度大于起始位置
                if (bsSrcString.Length > p_StartIndex)
                {
                    int p_EndIndex = bsSrcString.Length;

                    //当要截取的长度在字符串的有效长度范围内
                    if (bsSrcString.Length > (p_StartIndex + p_Length))
                    {
                        p_EndIndex = p_Length + p_StartIndex;
                    }
                    else
                    {   //当不在有效范围内时,只取到字符串的结尾

                        p_Length = bsSrcString.Length - p_StartIndex;
                        p_TailString = "";
                    }



                    int nRealLength = p_Length;
                    int[] anResultFlag = new int[p_Length];
                    byte[] bsResult = null;

                    int nFlag = 0;
                    for (int i = p_StartIndex; i < p_EndIndex; i++)
                    {

                        if (bsSrcString[i] > 127)
                        {
                            nFlag++;
                            if (nFlag == 3)
                            {
                                nFlag = 1;
                            }
                        }
                        else
                        {
                            nFlag = 0;
                        }

                        anResultFlag[i] = nFlag;
                    }

                    if ((bsSrcString[p_EndIndex - 1] > 127) && (anResultFlag[p_Length - 1] == 1))
                    {
                        nRealLength = p_Length + 1;
                    }

                    bsResult = new byte[nRealLength];

                    Array.Copy(bsSrcString, p_StartIndex, bsResult, 0, nRealLength);

                    myResult = Encoding.Default.GetString(bsResult);

                    myResult = myResult + p_TailString;
                }
            }

            return myResult;
        }

        /// <summary>
        /// 字符串如果超过指定长度则将超出的部分用指定字符串代替
        /// </summary>
        /// <param name="p_SrcString">原始字符串</param>
        /// <param name="p_Length">指定长度</param>
        /// <param name="p_TailString">用于替换的字符串</param>
        /// <returns>截取后的字符串</returns>
        public static string ToCut(this string p_SrcString, int p_Length, string p_TailString)
        {
            return ToCut(p_SrcString, 0, p_Length, p_TailString);
        }


        /// <summary>
        /// 返回 HTML 字符串的编码结果
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>编码结果</returns>
        public static string HtmlEncode(this string str)
        {
            return HttpUtility.HtmlEncode(str);
        }

        /// <summary>
        /// 返回 HTML 字符串的解码结果
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>解码结果</returns>
        public static string HtmlDecode(this string str)
        {
            return HttpUtility.HtmlDecode(str);
        }

        /// <summary>
        /// 返回 URL 字符串的编码结果
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>编码结果</returns>
        public static string UrlEncode(this string str)
        {
            return HttpUtility.UrlEncode(str);
        }

        /// <summary>
        /// 返回 URL 字符串的编码结果
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>解码结果</returns>
        public static string UrlDecode(this string str)
        {
            return HttpUtility.UrlDecode(str);
        }


        /// <summary>
        /// 获取站点根目录URL
        /// </summary>
        /// <returns>返回获取到的站点根目录URL</returns>
        public static string GetRootUrl(this string forumPath)
        {
            int port = HttpContext.Current.Request.Url.Port;
            return string.Format("{0}://{1}{2}{3}",
                                 HttpContext.Current.Request.Url.Scheme,
                                 HttpContext.Current.Request.Url.Host.ToString(),
                                 (port == 80 || port == 0) ? "" : ":" + port,
                                 forumPath);
        }


        /// <summary>
        /// 获取指定文件的扩展名,返回最后一个点后面的部分，如果没有点则返回""
        /// </summary>
        /// <param name="fileName">指定文件名</param>
        /// <returns>返回的扩展名</returns>
        public static string GetFileExtName(this string fileName)
        {
            if (fileName.StrIsNullOrEmpty() || fileName.IndexOf('.') <= 0)
                return "";

            fileName = fileName.ToLower().Trim();

            return fileName.Substring(fileName.LastIndexOf('.'), fileName.Length - fileName.LastIndexOf('.'));
        }



        /// <summary>
        /// 过滤用户输入的产品关键字（如：名片，宣传单，模板等）
        /// </summary>
        /// <param name="content">传入需要过滤的字符串</param>
        /// <returns>返回过滤后的搜索词</returns>
        public static string RemoveProName(this string content)
        {
            content = Regex.Replace(content, @"(宣传单|优惠券|会员卡|画册|不干胶贴|台历|手提袋|封套|挂历|折页|名片|模板)", "", RegexOptions.None);
            return content;
        }


        /// <summary>
        /// 根据Url地址获得页面中的源代码文件内容
        /// </summary>
        /// <param name="url">传入合法的Url地址</param>
        /// <returns>返回页面的代码文件内容</returns>
        public static string GetSourceTextByUrl(this string url)
        {
            WebRequest request = WebRequest.Create(url);
            request.Timeout = 20000;//20秒超时
            WebResponse response = request.GetResponse();

            Stream resStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(resStream);
            return sr.ReadToEnd();
        }

        /// <summary>
        /// 字段串是否为Null或为""(空)
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool StrIsNullOrEmpty(this string str)
        {
            if (str == null || str.Trim() == string.Empty)
                return true;

            return false;
        }


        ///// <summary>
        ///// 转换为简体中文
        ///// </summary>
        //public static string ToSChinese(this string str)
        //{
        //    return Strings.StrConv(str, VbStrConv.SimplifiedChinese, 0);
        //}

        ///// <summary>
        ///// 转换为繁体中文
        ///// </summary>
        //public static string ToTChinese(this string str)
        //{
        //    return Strings.StrConv(str, VbStrConv.TraditionalChinese, 0);
        //}


        private static byte[] Keys = { 0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F };
        /// <summary>
        /// AES加密字符串
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="encryptKey">加密密钥,要求为8位</param>
        /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
        public static string AESEncode(string encryptString, string encryptKey)
        {
            encryptKey = GetSubString(encryptKey, 32, "");
            encryptKey = encryptKey.PadRight(32, ' ');

            RijndaelManaged rijndaelProvider = new RijndaelManaged();
            rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32));
            rijndaelProvider.IV = Keys;
            ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor();

            byte[] inputData = Encoding.UTF8.GetBytes(encryptString);
            byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length);

            return Convert.ToBase64String(encryptedData);
        }

        /// <summary>
        /// AES解密字符串
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
        /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
        public static string AESDecode(string decryptString, string decryptKey)
        {
            try
            {
                decryptKey = GetSubString(decryptKey, 32, "");
                decryptKey = decryptKey.PadRight(32, ' ');

                RijndaelManaged rijndaelProvider = new RijndaelManaged();
                rijndaelProvider.Key = Encoding.UTF8.GetBytes(decryptKey);
                rijndaelProvider.IV = Keys;
                ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor();

                byte[] inputData = Convert.FromBase64String(decryptString);
                byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length);

                return Encoding.UTF8.GetString(decryptedData);
            }
            catch
            {
                return "";
            }

        }



        /// <summary>
        /// 如果输入的字符串时空，就返回缺省字符串
        /// </summary>
        /// <param name="str">输入字符串</param>
        /// <param name="defaultStr">缺省字符串</param>
        /// <returns>返回结果</returns>
        public static string ReturnDefaultRequired(this string str, string defaultStr)
        {
            if (string.IsNullOrEmpty(str))
            {
                return defaultStr.Trim();
            }

            return str.Trim();
        }

        /// <summary>
        /// 检测是否有Sql危险字符
        /// </summary>
        /// <param name="str">要判断字符串</param>
        /// <returns>true表示有，false表示没有</returns>
        public static bool IsSafeSqlString(this string str)
        {

            return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
        }


        ///// <summary>
        ///// 针对特殊字符串进行处理
        ///// </summary>
        //public static string sqlencode(string str)
        //{
        //    str = str.Replace("'", "''");
        //    //只在LIKE查询时才用到这些
        //    //			str=str.Replace("[","[[]"); //此句一定要在最先
        //    //			
        //    //			str=str.Replace("_","[_]");
        //    //			str=str.Replace("%","[%]");
        //    //			str=str.Replace("@#%nbsp","+");
        //    return str;
        //}


        /// <summary>
        /// 去掉数据后面的.00，比如3.00去掉.00得到3
        /// </summary>
        /// <param name="strNumber">传入需要去掉.00的数据</param>
        /// <returns>返回去掉.00后的字符串</returns>
        public static string SubLastThreeZero(this string strNumber)
        {
            if (strNumber != null && !string.IsNullOrEmpty(strNumber.Trim()))
            {
                string[] Nums = strNumber.Trim().Split('.');
                if (Nums.Length == 2)
                {
                    Byte[] NumBytes = Encoding.ASCII.GetBytes(Nums[1]);
                    Char[] NumChars = Encoding.ASCII.GetChars(NumBytes);
                    ArrayList arr = new ArrayList(NumChars);
                    for (int i = arr.Count; i > 0; i--)
                    {
                        if (arr[i - 1].ToString() == "0")
                        {
                            arr.RemoveAt(i - 1);
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (arr.Count > 0)
                    {
                        string Num1 = "";
                        for (int i = 0; i < arr.Count; i++)
                        {
                            Num1 += arr[i];

                        }
                        return Nums[0] + "." + Num1;
                    }
                    else
                    {
                        return Nums[0];
                    }
                }
                return strNumber.ToString();

            }
            return "";
        }


        /// <summary>
        /// 从HTML中获取文本,保留br,p,img
        /// </summary>
        /// <param name="HTML">传入的html字符串</param>
        /// <returns>返回过滤后的文体</returns>
        public static string GetTextFromHTML(this string HTML)
        {
            Regex regEx = new Regex(@"</?(?!br|/?p|img)[^>]*>", RegexOptions.IgnoreCase);

            return regEx.Replace(HTML, "");
        }

        /////<summary>
        ///// 过滤掉传入的指定的HTML的标签
        ///// </summary>
        ///// <param name="InputHTML">要过滤的HTML标签，以","分割</param>
        ///// <param name="Content">需要过滤的内容</param> 
        //public static string RemoveInputHtml(string Content, string InputHTML)
        //{
        //    Content = Content.ToLower();
        //    Regex regex;
        //    string[] InputHtmls = InputHTML.Split(',');
        //    for (int i = 0; i < InputHtmls.Length; i++)
        //    {
        //        if (!string.IsNullOrEmpty(InputHtmls[i]))
        //        {
        //            regex = new Regex(@InputHtmls[i], RegexOptions.IgnoreCase);
        //            Content = regex.Replace(Content, "");
        //        }
        //    }
        //    Content = Content.Replace(" ", "");
        //    Content = Content.Replace("&nbsp;", "");
        //    Content = Content.Replace("　", "");
        //    return Content;
        //}

        ///// <summary>
        ///// 为脚本替换特殊字符串
        ///// </summary>
        ///// <param name="str"></param>
        ///// <returns></returns>
        //public static string ReplaceStrToScript(string str)
        //{
        //    return str.Replace("\\", "\\\\").Replace("'", "\\'").Replace("\"", "\\\"");
        //}

        /// <summary>
        /// 移除Html中的标签，返回Html中的纯文本内容
        /// </summary>
        /// <param name="html">传入的html字符串</param>
        /// <returns>返回移除后的文本</returns>
        public static string RemoveHtml(this string html)
        {
            html = html.ToLower();
            Regex regex1 = new Regex(@"<script[\s\S]+</script *>", RegexOptions.IgnoreCase);
            Regex regex2 = new Regex(@" href *= *[\s\S]*script *:", RegexOptions.IgnoreCase);
            Regex regex3 = new Regex(@" no[\s\S]*=", RegexOptions.IgnoreCase);
            Regex regex4 = new Regex(@"<iframe[\s\S]+</iframe *>", RegexOptions.IgnoreCase);
            Regex regex5 = new Regex(@"<frameset[\s\S]+</frameset *>", RegexOptions.IgnoreCase);
            Regex regex6 = new Regex(@"\<img[^\>]+\>", RegexOptions.IgnoreCase);
            Regex regex7 = new Regex(@"</p>", RegexOptions.IgnoreCase);
            Regex regex8 = new Regex(@"<p>", RegexOptions.IgnoreCase);
            Regex regex9 = new Regex(@"<[^>]*>", RegexOptions.IgnoreCase);

            html = regex1.Replace(html, ""); //过滤<script></script>标记 
            html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性 
            html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件 
            html = regex4.Replace(html, ""); //过滤iframe 
            html = regex5.Replace(html, ""); //过滤frameset 
            html = regex6.Replace(html, ""); //过滤frameset 
            html = regex7.Replace(html, ""); //过滤frameset 
            html = regex8.Replace(html, ""); //过滤frameset 
            html = regex9.Replace(html, "");
            html = html.Replace(" ", "");
            html = html.Replace("</strong>", "");
            html = html.Replace("<strong>", "");
            html = html.Replace("&nbsp;", "");
            html = html.Replace("　", "");

            return html;
        }

        /// <summary>
        /// 检测是否是一幅图的Url
        /// </summary>
        /// <param name="strUrl">要验证的Url</param>
        /// <returns>true 表示是,false表示不是</returns>
        public static bool IsGaituURL(this string strUrl)
        {
            return Regex.IsMatch(strUrl, @"^(http|https)\://(([\s\S]+\.)|)+(yifutu.com)+([\s\S]*)");
        }



        ///// <summary>
        ///// 转换为string类型
        ///// </summary>
        ///// <param name="s">要转换的数据</param>
        ///// <param name="defValue">默认值</param>
        ///// <returns></returns>
        //public static string ToString(this float s, string defValue)
        //{
        //    return s.ToString(defValue);
        //}




        /*
         lp  2014年7月14日新增，用于IP转换查询
         */



        public static string IntToIp(long ipInt)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append((ipInt >> 24) & 0xFF).Append(".");
            sb.Append((ipInt >> 16) & 0xFF).Append(".");
            sb.Append((ipInt >> 8) & 0xFF).Append(".");
            sb.Append(ipInt & 0xFF);
            return sb.ToString();
        }

        #region Code by cyk 2015 04 28

        /// <summary>
        /// SQL删除条件验证
        /// </summary>
        /// <param name="str">要判断字符串</param>
        /// <returns>true通过验证 false没通过验证</returns>
        public static bool SqlFilter_Del(this string str)
        {
            return str.IsIDs();
        }

        /// <summary>
        /// SQL查询条件验证
        /// </summary>
        /// <param name="str">要判断字符串</param>
        /// <returns>true通过验证 false没通过验证</returns>
        public static bool SqlFilter_List(this string str)
        {
            return str == str.SqlCheck();
        }

        /// <summary>
        /// 验证是否是英文逗号分割的ID（例如：123,123或123）
        /// </summary>
        /// <param name="str">要判断字符串</param>
        /// <returns>true通过验证 false没通过验证</returns>
        public static bool IsIDs(this string str)
        {
            return Regex.IsMatch(str, @"^((\d+)[,])+(\d+)$|^(\d+)$");
        }


        ///// <summary>
        ///// 验证身份
        ///// </summary>
        ///// <param name="str">要判断字符串</param>
        ///// <param name="ciphertext">密文</param>
        ///// <returns>true通过验证 false没通过验证</returns>
        //public static bool VCodeCheck(this string str, string ciphertext)
        //{
        //    try
        //    {
        //        string code = ciphertext.DESDecode(runtimeProperties.Encryptkey);
        //        int expires = code.Substring(code.LastIndexOf('S') + 1).ToInt32(0);
        //        code = code.Substring(0, code.LastIndexOf('S').ToInt32(0));
        //        string time = code.Substring(code.Length - 12);
        //        return code.Substring(0, code.Length - 12) == (str + time).ToSHA256().ToMD5() && long.Parse(time) > long.Parse(DateTime.Now.ToString("yyyyMMddhhmm"));
        //    }
        //    catch (Exception)
        //    {
        //        return false;
        //    }
        //}
        #endregion


        /// <summary>
        /// 字节转kb、mb、gb
        /// </summary>
        /// <param name="KSize"></param>
        /// <returns></returns>
        public static string ByteConversionGBMBKB(Int64 KSize)
        {
            const int GB = 1024 * 1024 * 1024;//定义GB的计算常量
            const int MB = 1024 * 1024;//定义MB的计算常量
            const int KB = 1024;//定义KB的计算常量
            if (KSize / GB >= 1)//如果当前Byte的值大于等于1GB
                return (Math.Round(KSize / (float)GB, 2)).ToString() + "GB";//将其转换成GB
            else if (KSize / MB >= 1)//如果当前Byte的值大于等于1MB
                return (Math.Round(KSize / (float)MB, 2)).ToString() + "MB";//将其转换成MB
            else if (KSize / KB >= 1)//如果当前Byte的值大于等于1KB
                return (Math.Round(KSize / (float)KB, 2)).ToString() + "KB";//将其转换成KGB
            else
                return KSize.ToString() + "Byte";//显示Byte值
        }



        #region string扩展方法，根据字节数截取字符串
        /// <summary>
        /// 根据限制字节数截取字符串
        /// </summary>
        /// <param name="inputStr">源字符串</param>
        /// <param name="startIndex">开始索引</param>
        /// <param name="replaceStr">超出限制的替换文本</param>
        /// <param name="charLength">限制的字节数</param>
        /// <returns></returns>
        public static string SplitStr4Byte(this string inputStr, int startIndex, string replaceStr, int charLength)
        {
            if (inputStr == null || inputStr.Trim() == "")
            {
                return "";
            }
            if (Encoding.Default.GetByteCount(inputStr) < startIndex + 1 + charLength)
            {
                return inputStr;
            }
            int i = 0;//字节数
            int j = 0;//实际截取长度
            foreach (char newChar in inputStr)
            {
                if ((int)newChar > 127)
                {
                    //汉字
                    i += 2;
                }
                else
                {
                    i++;
                }
                if (i > startIndex + charLength)
                {
                    inputStr = inputStr.Substring(startIndex, j) + "...";
                    break;
                }
                if (i > startIndex)
                {
                    j++;
                }
            }
            return inputStr;
        }
        #endregion

        /// <summary>
        /// 转换特殊符号为HTML可显示的编码 
        /// </summary>
        public static string ToHtml(this string s)
        {
            string regStr = string.Empty;
            regStr = "\"";
            Regex objRegex = new Regex(regStr, RegexOptions.IgnoreCase);
            s = objRegex.Replace(s, "&quot;");
            regStr = "&";
            objRegex = new Regex(regStr, RegexOptions.IgnoreCase);
            s = objRegex.Replace(s, "&amp;");
            regStr = "<";
            objRegex = new Regex(regStr, RegexOptions.IgnoreCase);
            s = objRegex.Replace(s, "&lt;");
            regStr = ">";
            objRegex = new Regex(regStr, RegexOptions.IgnoreCase);
            s = objRegex.Replace(s, "&gt;");
            regStr = " ";
            objRegex = new Regex(regStr, RegexOptions.IgnoreCase);
            s = objRegex.Replace(s, "&nbsp;");
            regStr = "‘";
            objRegex = new Regex(regStr, RegexOptions.IgnoreCase);
            s = objRegex.Replace(s, "&lsquo;");
            regStr = "’";
            objRegex = new Regex(regStr, RegexOptions.IgnoreCase);
            s = objRegex.Replace(s, "&rsquo;");
            regStr = "“";
            objRegex = new Regex(regStr, RegexOptions.IgnoreCase);
            s = objRegex.Replace(s, "&ldquo;");
            regStr = "”";
            objRegex = new Regex(regStr, RegexOptions.IgnoreCase);
            s = objRegex.Replace(s, "&rdquo;");
            return s;
        }

        /// <summary>
        /// 获取RUL指定参数值
        /// </summary>
        /// <param name="url">URL</param>
        /// <param name="parmName">参数名</param>
        /// <param name="defaultReturn">未匹配上的默认值</param>
        /// <returns></returns>
        public static string GetUrlParam(this string url, string parmName, string defaultReturn)
        {
            string regex = "(^?|&)" + parmName + "=([^&]*)(&|$)";
            Match m = Regex.Match(url, regex);
            return Regex.IsMatch(url, regex) ? m.Groups[2].Value : defaultReturn;
        }


        /// <summary>
        /// 获取汉字首字母（可包含多个汉字）
        /// </summary>
        /// <param name="strText"></param>
        /// <returns></returns>
        public static string GetChineseFirstSpell(this string strText)
        {
            int len = strText.Length;
            string myStr = "";
            for (int i = 0; i < len; i++)
            {
                myStr += getSpell(strText.Substring(i, 1));
            }
            return myStr;
        }

        ///// <summary>
        ///// 将Json字符串转为T类泛型集合（注意，每一类json值必须用 [ ] 括起来，否则会拿到空）
        ///// </summary>
        ///// <typeparam name="T">实体类型</typeparam>
        ///// <param name="json">json字符串</param>
        ///// <returns></returns>
        //public static List<T> Json2List<T>(this string json)
        //{
        //    JavaScriptSerializer jss = new JavaScriptSerializer();
        //    return jss.Deserialize<List<T>>(json);
        //}

        /// <summary>
        /// 字符串表去首尾空格
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string StrTrim(this string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            else
            {
                return str.Trim();
            }
        }

        private static string getSpell(string cnChar)
        {
            byte[] arrCN = Encoding.Default.GetBytes(cnChar);
            if (arrCN.Length > 1)
            {
                int area = (short)arrCN[0];
                int pos = (short)arrCN[1];
                int code = (area << 8) + pos;
                int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
                for (int i = 0; i < 26; i++)
                {
                    int max = 55290;
                    if (i != 25) max = areacode[i + 1];
                    if (areacode[i] <= code && code < max)
                    {
                        return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
                    }
                }
                return "*";
            }
            else
                return cnChar;
        }

        /// <summary>
        /// 防止sql注入的字符串
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string FilterSql(string s)
        {
            if (string.IsNullOrEmpty(s)) return string.Empty;
            s = Regex.Replace(s, @"'", "", RegexOptions.IgnoreCase);
            s = Regex.Replace(s, @";", "", RegexOptions.IgnoreCase);
            s = Regex.Replace(s, @" or ", "", RegexOptions.IgnoreCase);
            s = Regex.Replace(s, @"select ", "", RegexOptions.IgnoreCase);
            s = Regex.Replace(s, @"update ", "", RegexOptions.IgnoreCase);
            s = Regex.Replace(s, @"insert ", "", RegexOptions.IgnoreCase);
            s = Regex.Replace(s, @"delete ", "", RegexOptions.IgnoreCase);
            s = Regex.Replace(s, @"exec ", "", RegexOptions.IgnoreCase);
            s = Regex.Replace(s, @"drop ", "", RegexOptions.IgnoreCase);
            s = Regex.Replace(s, @"create ", "", RegexOptions.IgnoreCase);
            s = Regex.Replace(s, @"%", "", RegexOptions.IgnoreCase);
            s = Regex.Replace(s, @"--", "", RegexOptions.IgnoreCase);
            return s;
        }

        /// <summary>
        /// 判断给定的str在不在list里
        /// </summary>
        /// <param name="str"></param>
        /// <param name="list"></param>
        /// <returns>true 在、false 不在</returns>
        public static bool IsContainsStr(this string str, List<string> list)
        {
            if (list.HasData())
            {
                if (list.Contains(str))
                {
                    return true;
                }
            }
            return false;
        }
    }
}
